<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('announcements', function (Blueprint $table) {
            $table->id();
            $table->string('title')->comment('公告标题');
            $table->text('content')->comment('公告内容');
            $table->enum('type', ['notice', 'news', 'policy', 'reminder', 'other'])->default('notice')->comment('公告类型');
            $table->enum('priority', ['low', 'normal', 'high', 'urgent'])->default('normal')->comment('优先级');
            $table->enum('status', ['draft', 'published', 'archived'])->default('draft')->comment('状态');
            $table->boolean('is_pinned')->default(false)->comment('是否置顶');
            $table->timestamp('published_at')->nullable()->comment('发布时间');
            $table->timestamp('expired_at')->nullable()->comment('过期时间');
            $table->unsignedBigInteger('created_by')->comment('创建人ID');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('更新人ID');
            $table->timestamps();

            // 外键约束
            $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            // 索引
            $table->index(['status', 'is_pinned']);
            $table->index('published_at');
            $table->index('expired_at');
            $table->index('type');
            $table->index('priority');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('announcements');
    }
};
